package com.tyd.cams;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tyd.cams.Adapter.ZycjStuListAdapter;
import com.tyd.cams.entity.LoginBean;
import com.tyd.cams.entity.StuCjbean;
import com.tyd.cams.entity.User;
import com.tyd.cams.entity.ZycxStuListBeam;
import com.tyd.cams.utils.SPUtils;
import com.xuexiang.xui.widget.dialog.DialogLoader;
import com.xuexiang.xui.widget.dialog.strategy.IDialogStrategy;
import com.xuexiang.xui.widget.dialog.strategy.impl.MaterialDialogStrategy;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;

import static com.xuexiang.xui.XUI.getContext;

public class ZycjcxActivity extends AppCompatActivity {
    private SimpleAdapter adapter;
    private ListView stulist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zycjcx);
       /* SharedPreferences SPF = getSharedPreferences("SP", MODE_PRIVATE);
        String userInfo = SPF.getString("userInfo", "");
         User user1 = gs.fromJson(userInfo, User.class);
        */

        User user = new Gson().fromJson(SPUtils.getUserInfo(), User.class);
        if (user.getUsertype() != 1) {
            Log.i("UserInfo", user.toString());
//            Toast.makeText(ZycjcxActivity.this, "学生查询成绩", Toast.LENGTH_SHORT).show();
            getXscj(user);
//            this.onDestroy();
        } else {
            getZycjList();
        }
    }

    private void getXscj(User user) {
        new Thread(() -> OkHttpUtils.post()
                .url(SPUtils.getIP() + "/result/mtgetResul")
                .addHeader("Authorization", SPUtils.getToken())
                .addParams("personId", user.getAccount())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(ZycjcxActivity.this, "查询成绩超时" + user.getUsername(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("获取的返回信息", response);
                        Gson son = new Gson();
                        List<StuCjbean> stuCjbeans = son.fromJson(response, new TypeToken<List<StuCjbean>>() {
                        }.getType());
//                        Toast.makeText(getContext(), "学生成绩" + stuCjbeans.toString(), Toast.LENGTH_SHORT).show();
                        final EditText editText = new EditText(getContext());
                        for (StuCjbean cjbean : stuCjbeans) {
                            editText.setText(editText.getText() + "课程：" + cjbean.getCourseName() + "\t\t" + "成绩:" + cjbean.getScore() + "\n\n\t");
                            editText.setEnabled(false);
                        }
                        IDialogStrategy mDialogLoader = DialogLoader.getInstance().setIDialogStrategy(new MaterialDialogStrategy());
                        mDialogLoader.showTipDialog(ZycjcxActivity.this,
                                R.drawable.cjcx,
                                user.getUsername() + "的成绩",
                                editText.getText().toString(),
                                "确认",
                                (dialog, which) -> {
                                    //点击确认按钮后关闭提示框
                                    dialog.dismiss();
                                    Intent intent = new Intent(ZycjcxActivity.this, MainsActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    startActivity(intent);


                                });
                    }
                })).start();
    }

    /*成绩查询List获取开始*/
    private void getZycjList() {
        new Thread() {
            public void run() {
                OkHttpUtils.post()
                        .url(SPUtils.getIP() + "/result/mtgetstulist")
                        .addHeader("Authorization", SPUtils.getToken())
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                Toast.makeText(ZycjcxActivity.this, "专业成绩查询链接服务器超时", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                Log.i("获取的返回信息", response);
                                Gson son = new Gson();
                                List<ZycxStuListBeam> zycxStuListBeamList = son.fromJson(response, new TypeToken<List<ZycxStuListBeam>>() {
                                }.getType());
                                ZycjStuListAdapter zycjStuListAdapter = new ZycjStuListAdapter(ZycjcxActivity.this, zycxStuListBeamList);
                                ListView listView = findViewById(R.id.stuInfolistview);
                                listView.setAdapter(zycjStuListAdapter);
                                System.out.println("学生列表查询成功" + zycxStuListBeamList.toString());
                            }
                        });
            }
        }.start();

    }
}